package com.qys.livesMall.security.controller;

import com.qys.livesMall.app.user.model.dto.app.AppLoginDTO;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.constants.SysConstants;
import com.qys.livesMall.common.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *  app登录
 * @date 2023/05/22 15:59
 */
@RestController
@RequestMapping("/app")
@Tag(name = "APP-用户登录")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppLoginController {

    private final IH2AppUserService appUserService;

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R<String> login(@RequestBody @Validated AppLoginDTO loginDTO, HttpServletRequest request) {
        return R.ok(appUserService.login(loginDTO, request));
    }

    /**
     * 谷歌登录接口
     *
     * @param code 授权码/google用户唯一标识
     * @return
     */
    @GetMapping("/googleLogin")
    @ResponseBody
    @Operation(summary = "谷歌登录")
    public R<Map<String,String>> loginByGoogle(@RequestParam String code, HttpServletRequest request) {
        Map<String, String> map;
        try {
            map = appUserService.loginByGoogle(code, request);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok(map);
    }

    /**
     * 脸书登录接口
     * @param userId    facebook用户id
     * @param userToken facebook用户token
     * @param request   http请求
     * @return
     */
    @GetMapping("/facebookLogin")
    @ResponseBody
    @Operation(summary = "脸书登录")
    public R<Map<String,String>> facebookLogin(@RequestParam String userId, @RequestParam String userToken, HttpServletRequest request) {
        Map<String, String> map;
        try {
            map = appUserService.facebookLogin(userId, userToken, request);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok(map);
    }

    /**
     * 登出
     *
     * @param request
     * @return
     */
    @PostMapping("/loginOut")
    public R<Void> loginOut(HttpServletRequest request) {
        String token = request.getHeader(SysConstants.AUTHORIZATION);
        appUserService.loginOut(token);
        return R.ok();
    }
}

